package com.nf.health.app.healthbank.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.healthbank.MyHealthBankDetailActivity;
import com.nf.health.app.healthbank.adapter.MyHealthyBankAdapter;
import com.nf.health.app.healthbank.bean.HealthBankVo;
import com.nf.health.app.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/10/8.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description 健康银行-->治未病科 ,与公共卫生分开,为以后的单独逻辑做准备
 * @email he_pann@163.com
 */
public class PublicHealthFragment extends IBaseFragment {

    private static final String GET_HEALTH_BANK_LIST = "get_health_bank_list";
    private static final String DEL_ATTENTION_BANK = "delAttentionBank";
    private static final String ADD_ATTENTION_BANK = "addAttentionBank";

    @Bind(R.id.dlv_health_bank)
    DragListView dlvHealthBank;

    private int page = 1;
    private int pageSize = 20;
    private boolean isLoadMore = false;//记录加载更多
    private List<HealthBankVo> mHealthBankList = new ArrayList<>();
    private MyHealthyBankAdapter mAdapter;
    private int attentionPosition = -1;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //复用治未病科的布局
        addViewFillInRoot(R.layout.fragment_treat_disease);
        ButterKnife.bind(this, rootView);
        initListener();
        initData(GET_HEALTH_BANK_LIST);
        dlvHealthBank.setRefreshableAndLoadMoreable(true, true);
    }

    private void initData(String flag) {
        provider.requestHealthBank(page + "", pageSize + "", "2", flag);
    }

    private void initListener() {

        dlvHealthBank.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                page = 1;
                isLoadMore = false;
                initData(GET_HEALTH_BANK_LIST);
            }

            @Override
            public void onLoadMore() {
                page++;
                isLoadMore = true;
                initData(GET_HEALTH_BANK_LIST);
            }
        });
        dlvHealthBank.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString(MyHealthBankDetailActivity.DOCTOR_ID, mHealthBankList.get(position-1).getDoctorid());
                bundle.putString(MyHealthBankDetailActivity.DOCTOR_DETAIL_TITLE, mHealthBankList.get(position-1).getNick());
                ActivityUtils.showActivity(getActivity(), MyHealthBankDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (GET_HEALTH_BANK_LIST.equals(actionName)) {
            dlvHealthBank.completeRefresh();
            dlvHealthBank.completeLoadMore();
            List<HealthBankVo> newListBean = (List<HealthBankVo>) object;
            if (newListBean.size() == 0) {
                showToast("没有更多数据了");
                return;
            }
            if (!isLoadMore) {
                mHealthBankList.clear();
            }
            mHealthBankList.addAll(newListBean);
            if (mAdapter == null) {
                mAdapter = new MyHealthyBankAdapter(getActivity(), mHealthBankList);
                dlvHealthBank.setAdapter(mAdapter);
                mAdapter.setOnMyClickListener(new OnMyClickListener() {
                    @Override
                    public void onClick(int position, String flag) {
                        handleClick(position);
                    }
                });
            }
            mAdapter.notifyDataSetChanged();
        }  else if (ADD_ATTENTION_BANK.equals(actionName)) {
            try {
                HealthBankVo bean = mHealthBankList.get(attentionPosition);
                bean.setIsfollow("y");
                showToast("关注成功");
                mAdapter.notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (DEL_ATTENTION_BANK.equals(actionName)) {
            try {
                HealthBankVo bean = mHealthBankList.get(attentionPosition);
                bean.setIsfollow("n");
                showToast("取消关注");
                mAdapter.notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 处理点击事件,关注,取消关注
     *
     * @param position
     */
    private void handleClick(int position) {
        HealthBankVo bean = mHealthBankList.get(position);
        attentionPosition = position;
        if ("y".equals(bean.getIsfollow())) {
            //已经关注了就取消关注
            provider.delAttentionBank(bean.getDoctorid(), DEL_ATTENTION_BANK);
        } else {
            provider.addAttentionBank(bean.getDoctorid(), ADD_ATTENTION_BANK);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initData(GET_HEALTH_BANK_LIST);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
